/* 
 *  Document   : jquery.navigation
 *  Created on : 2011-12-19, 22:19:39
 *  Author     : GodSon
 *  Email      : wmails@126.com
 *  Link       : www.btboys.com 
 *  
 */

/**
 * navigationc导航插件
 */
(function($){
    function init(target){
        if($(target).attr("class") == "navigation-sep"){
            $(target).html("&nbsp;"); 
            return;
        }        
        var opts = $.data(target, 'navigation').options;
        $(target).addClass("easyui-navigation-item").unbind().hover(function(){
            $(this).addClass("easyui-navigation-over");
        },function(){
            $(this).removeClass("easyui-navigation-over");
        });

        $(target).bind("click",function(){
            if(!opts.parent){
                var title = $(this).attr("title");
                var p =  $(target).closest("div.easyui-navigation");
                p.find(".easyui-navigation-select").removeClass("easyui-navigation-select");
                $(this).addClass("easyui-navigation-select");
                if(opts.onClick) {
                    opts.onClick.call(this,$(this).attr("title"));
                }else{
                    var href = $(this).attr("href");
                    if(href){
                        if(opts.tabTarget){
                            if ($(opts.tabTarget).tabs('exists',title)){
                                $(opts.tabTarget).tabs('select', title);
                            } else {
                                $(opts.tabTarget).tabs("add",{
                                    title:title,
                                    iconCls:opts.iconCls,
                                    href:href,
                                    closable:true
                                });
                            }
                            $(opts.tabTarget).data("tabs").options.onContextMenu = function(e,title){
                               var tmenu = createTabMenu($(this),title);
                                e.preventDefault();
                                tmenu.menu('show', {
                                    left:e.pageX,
                                    top:e.pageY
                                });
                            }
                        }else if(opts.layoutTarget){
                            $(opts.layoutTarget).layout("panel","center").panel("refresh",href).panel("setTitle",title);
                        }
                    }
                }
            }else{
                var lef = $(target).children().eq(0);
                
                if(lef.hasClass("datagrid-row-expand")){
                    lef.removeClass("datagrid-row-expand");
                    lef.addClass("datagrid-row-collapse");
                    $("div[parentTarget="+opts.parent+"]").slideDown();
                }else{
                    lef.removeClass("datagrid-row-collapse");
                    lef.addClass("datagrid-row-expand");
                    $("div[parentTarget="+opts.parent+"]").slideUp();
                }
            }
        });
        
        var span = $('<span class="l-btn-text"></span>').html(opts.title);
        if(opts.iconCls) span.addClass(opts.iconCls).css("padding","0 0 2px 20px");
        
        $(target).html(span);
        if(opts.parent){
            var children = $("<div/>").attr("parentTarget", opts.parent).css("display",!opts.open ? "none" : "block");
            $(target).prepend("<span style='padding-right:16px' class="+(!opts.open ? "datagrid-row-expand" : "datagrid-row-collapse")+"></span>").after(children);
        }   
        if(opts.ptarget)   $(target).appendTo($("div[parentTarget="+opts.ptarget+"]")).prepend("<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>");
    }
    
    function createTabMenu(tabsTaget,title){
        var tmenu;
        if (!$('#tabs-ContextMenu').length){
            tmenu = $('<div/>').attr("id", "tabs-ContextMenu").css("width","100").appendTo('body');
            $('<div/>').attr("iconCls", "icon-cancel").attr("handler", "close").html("关闭").appendTo(tmenu);
            $('<div/>').addClass("menu-sep").appendTo(tmenu);
            $('<div/>').attr("iconCls", "icon-cancel").attr("handler", "closeAll").html("关闭所有").appendTo(tmenu);
            $('<div/>').attr("iconCls", "icon-cancel").attr("handler", "closeOther").html("关闭其他").appendTo(tmenu);
        }else{
            tmenu = $('#tabs-ContextMenu');
        }
        tmenu.menu({
            onClick: function(item){
                closeTab($(item.target).attr("handler"),tabsTaget,title);
            }
        });
        return tmenu;
    }
    
    function closeTab(handler,tabsTaget,title){
        var alltabs = tabsTaget.tabs("tabs");
        var opt = tabsTaget.tabs("getTab",title).panel('options');
        var titiles = [];
            
        if(handler == "close"){
            if(!opt.closable) return;
            titiles.push(opt.title);
        }else if(handler == "closeAll"){
            $.each(alltabs,function(i,tab){
                var o = tab.panel('options');
                if(o.closable){
                    titiles.push(o.title);
                } 
            });
        }else{
            $.each(alltabs,function(i,tab){
                var o = tab.panel('options');
                if(o.closable && (o.title != opt.title)){
                    titiles.push(o.title);
                }
            });
        }
        //关闭选项卡
        for ( var i = 0; i < titiles.length; i++) {
            tabsTaget.tabs('close',titiles[i]);
        }
    }
    
    function parseOptions(target){
        var t = $(target);
        return {
            iconCls: (t.attr('iconCls') || undefined),
            href: (t.attr('href') || undefined),
            title:(t.attr('title') || "标题"),
            parent:(t.attr('parent') || false),
            ptarget:(t.attr('ptarget') || false),
            open:(t.attr('expand') || false)
        };
    }
    
    function parseNvOptions(target){
        var t = $(target);
        return {
            layoutTarget: (t.attr('layoutTarget') || undefined),
            tabTarget:(t.attr('tabTarget') || undefined)
        };
    }
        
    $.fn.navigation = function(options){     
       
        return this.each(function(){
            var container = this;
            options = $.extend({}, $.fn.navigation.defaults,options,parseNvOptions(container))
            $(container).panel({
                fit:true,
                border:false,
                bodyCls:"easyui-navigation"
            });
            var items = $(container).children();
            items.each(function(){
                $.data(this, 'navigation', {
                    options: $.extend({}, options, parseOptions(this))
                });
                init(this); 
            });

            var first =items.first();
            if(first.attr("parent")){
                if(!first.attr("expand")){
                    first.click();
                }
                $("div[parentTarget="+first.attr("parent")+"]").children().first().click();
            }else if(options.layoutTarget &&　options.tabTarget){
                first.click();
            }
        });
    }
    $.fn.navigation.defaults = {
        layoutTarget: null,
        tabTarget: null
    };
    
    $.parser.plugins.push("navigation");
})(jQuery);